<?php

namespace App\Http\Controllers\System;

use App\Common\Autoinject;
use App\Enums\System\DelFlag;
use App\Http\Requests\System\SysLogin\LoginRequest;
use App\Models\BaseModel;
use App\Models\System\SysConfigModel;
use App\Models\System\SysDictDataModel;
use App\Models\System\SysUserModel;
use App\Services\System\SysConfigService;
use App\Services\System\SysLoginService;
use App\Services\System\SysMenuService;
use App\Services\System\SysPermissionService;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
use OpenApi\Attributes as OA;
use ReflectionClass;
use ReflectionEnumUnitCase;
use ReflectionObject;
use ruoyi\admin\facades\Redis as FacadesRedis;
use TypeError;

class SysLoginController extends \App\Http\Controllers\Controller {

    #[Autoinject]
    private SysLoginService $loginService;
    #[Autoinject]
    private SysMenuService $menuService;
    #[Autoinject]
    private SysPermissionService $permissionService;

    #[OA\Post(
        '/admin/login', '/admin/login', tags: ['后端/账号'], 
        summary: '账号登录', description: '账号密码登录 验证码开关后台下发', 
        requestBody: new OA\RequestBody(content: new OA\MediaType('application/x-www-form-urlencoded', 
            new OA\Schema(ref: '#/components/schemas/LoginRequest'),
        )),
        responses: [
            new OA\Response(response: 200, description: 'Success、参数/后台异常', content: new OA\JsonContent(
                oneOf:[
                    new OA\Schema(title: '请求成功', required: ['code', 'msg', 'token'], properties: [
                        new OA\Property('code', title: '状态码', type: 'int', example: 200),
                        new OA\Property('msg', title: '请求结果', type: 'string', example: '请求成功 !'),
                        new OA\Property('token', title: 'bearer token', type: 'string', example: '9|IuAolps29uEmF2WqsjsIskH4I9s2WaO9yyRHxcXz'),
                    ]),
                    new OA\Schema(ref: '#/components/schemas/Service500ErrBean'),
                ]
            )),
            new OA\Response(response: 500, description: '服务器异常', content: new OA\MediaType('text/plain', example: '空/错误堆栈')),
        ],
    )]
    public function login(LoginRequest $request, SysConfigService $service, SysConfigModel $model) {
        $token = $this->loginService->login($request);
        
        return show_response(['token'=>$token]);
    }

    
    #[OA\Get(
        '/admin/getInfo', '/admin/getInfo', tags: ['后端/账号'], 
        summary: '获取用户信息', description: '', 
        responses: [
            new OA\Response(response: 200, description: 'Success、参数/后台异常', content: new OA\JsonContent(
                oneOf:[
                    new OA\Schema(title: '请求成功', required: ['code', 'msg', 'token'], properties: [
                        new OA\Property('code', title: '状态码', type: 'int', example: 200),
                        new OA\Property('msg', title: '请求结果', type: 'string', example: '请求成功 !'),
                        new OA\Property('token', title: 'bearer token', type: 'string', example: '9|IuAolps29uEmF2WqsjsIskH4I9s2WaO9yyRHxcXz'),
                    ]),
                    new OA\Schema(ref: '#/components/schemas/Service500ErrBean'),
                ]
            )),
            new OA\Response(response: 500, description: '服务器异常', content: new OA\MediaType('text/plain', example: '空/错误堆栈')),
        ],
    )]
    public function getInfo() {
        $user = user();
        // $data['user'] = $user->commUser();
        $data['user'] = $user->selectUserById($user->getId());
        $data['roles'] = SysPermissionService::getRolePermission($user);
        $data['permissions'] = SysPermissionService::getMenuPermission($user);

        return show_response($data);

        // $filter = ['create_by', 'create_time', 'update_by', 'update_time'];
        // $user = array_filter(request()->user()->toArray(), fn($key) => !in_array($key, $filter), ARRAY_FILTER_USE_KEY);
        // $user['dept'] = request()->user()->dept;
        // $user['roles'] = request()->user()->roles;
        // // $user['permissions'] = set()
        // return $this->bean(['user'=>$user]);
    }

    #[OA\Get(
        '/admin/unauth', '/admin/unauth', tags: ['后端/账号'], 
        summary: '账号认证失败', description: '', 
        responses: [
            new OA\Response(response: 200, description: 'Success、参数/后台异常', content: new OA\JsonContent(
                oneOf:[
                    new OA\Schema(title: '请求成功', required: ['code', 'msg'], properties: [
                        new OA\Property('code', title: '状态码', type: 'int', example: 410),
                        new OA\Property('msg', title: '请求结果', type: 'string', example: '令牌已过期, 请重新登录 ! / 用户未登录 !'),
                    ]),
                    new OA\Schema(ref: '#/components/schemas/Service500ErrBean'),
                ]
            )),
            new OA\Response(response: 500, description: '服务器异常', content: new OA\MediaType('text/plain', example: '空/错误堆栈')),
        ],
    )]
    public function unauth() {
        $msg = request()->hasHeader('authorization') ? '令牌已过期, 请重新登录 !' : '用户未登录 !';
        
        return show_response(msg:$msg, code:410);
    }



    #[OA\Post(
        '/admin/logout', '/admin/logout', tags: ['后端/账号'], 
        summary: '账号退出', description: '', 
        requestBody: new OA\RequestBody(content: new OA\MediaType('application/x-www-form-urlencoded', 
        )),
        responses: [
            new OA\Response(response: 200, description: 'Success、参数/后台异常', content: new OA\JsonContent(
                oneOf:[
                    new OA\Schema(title: '请求成功', required: ['code', 'msg'], properties: [
                        new OA\Property('code', title: '状态码', type: 'int', example: 200),
                        new OA\Property('msg', title: '请求结果', type: 'string', example: '请求成功 !'),
                    ]),
                    new OA\Schema(ref: '#/components/schemas/Service500ErrBean'),
                ]
            )),
            new OA\Response(response: 500, description: '服务器异常', content: new OA\MediaType('text/plain', example: '空/错误堆栈')),
        ],
    )]
    public function logout() {
        return show_response();
    }



    // 获取路由信息
    public function getRouters() {
        $menus = $this->menuService->selectMenuTreeByUserId(user());
        $menus = $this->menuService->buildMenus($menus);
        
        return show_response(['data' => $menus]);
    }



}
